// pages/user/user.js
var QR = require("../../utils/qrcode.js");
var util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "李小成",
    code: "",
    effectiveness: 'wodhso',
    screenBrightness: 0
  },
  //适配不同屏幕大小的canvas
  setCanvasSize: function() {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 430;//不同屏幕下canvas的适配比例；设计稿是750宽
      var width = res.windowWidth / scale;
      var height = width;//canvas画布为正方形
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  createQrCode: function(url, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片
    QR.qrApi.draw(url, canvasId, cavW, cavH);
    var that = this;
    //二维码生成之后调用canvasToTempImage();延迟3s，否则获取图片路径为空
    var st = setTimeout(function() {
      that.canvasToTempImage();
      clearTimeout(st);
    }, 500);

  },
  //获取临时缓存照片路径，存入data中
  canvasToTempImage: function() {
    var that = this;
    wx.canvasToTempFilePath({
      canvasId: 'mycanvas',
      success: function(res) {
        var tempFilePath = res.tempFilePath;
        console.log(tempFilePath);
        that.setData({
          imagePath: tempFilePath,
        });
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  //点击图片进行预览，长按保存分享图片
  previewImg: function (e) {
    var img = this.data.imagePath
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
  changeToNew: function () {
    var that = this;
    wx.redirectTo({
      url: '../new/new?screenBrightness=' + that.data.screenBrightness,
    })
  },
  changeToSetting: function () {
    var that = this;
    wx.navigateTo({
      url: '../setting/setting?screenBrightness=' + that.data.screenBrightness,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getScreenBrightness({
      success: function (res) {
        that.setData({
          screenBrightness: res.value,
        });
      }
    });
    var userName = wx.getStorageSync('userName') || '';
    that.setData({
      name: userName,
    });
    if (userName == '') {
      wx.redirectTo({
        url: '../new/new?screenBrightness=' + that.data.screenBrightness,
      })
    } else {
      wx.showToast({
        title: '生成中...',
        icon: 'loading',
        duration: 200
      });

      // var timestamp = Date.parse(new Date()) / 1000;
      // var duration = util.PrefixInteger(0 * 60 * 60 + '1' * 60, 5);
      // var word = '1234567890' + '' + timestamp + '' + duration + util.formatNumber('2') + '00000';
      // var words = util.EncryptQR('1234567890', 0, 1, '2');
      // console.log('timestamp=' + timestamp);
      // console.log('duration=' + duration);
      // console.log('word=' + word);
      // console.log('enword=' + words);
      // console.log('deword1=' + util.Encrypt('12345678901539817578000600200000'));
      // console.log('deword2=' + util.Decrypt('55404b9ea091387b266703940fbacb118c768b648d28280b11f2fdec6d17588d'));
      // console.log('deword3=' + util.Decrypt('55404b9ea091387b266703940fbacb118c768b648d28280b11f2fdec6d17588df466fb6de91c66a374d64a54628b419c'));
      
      var st = setTimeout(function () {
        wx.hideToast()
        var size = that.setCanvasSize();
        //绘制二维码
        that.createQrCode(util.EncryptQR(wx.getStorageSync('guardCardNo'),'1','0','0'), "mycanvas", size.w, size.h);
        that.setData({
          maskHidden: true
        });
        clearTimeout(st);
      }, 200)

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (wx.getStorageSync('userName') || '' != '') {
      wx.setScreenBrightness({
        value: 1
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    var that = this;
    wx.setScreenBrightness({
      value: that.data.screenBrightness
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    var that = this;
    wx.setScreenBrightness({
      value: that.data.screenBrightness
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})